using System.Linq;
using Firestarter.DB.Schema.BO;

namespace Firestarter.DB.Schema.Logic
{

    public interface IDBPostProcessor
    {
        void Process(IDBDatabase database);
        void ProcessColumn(DBColumn dbColumn);
    }
    public class DBDefaultPostProcessor : IDBPostProcessor
    {
        public void Process(IDBDatabase database)
        {
            //Should Do nothing By Default.
        }

        public void ProcessColumn(DBColumn dbColumn)
        {
            //Do Nothing By Default.
        }
    }
    public class DBMySqlPostProcessor : IDBPostProcessor
    {
        public void Process(IDBDatabase database)
        {
            if (database == null) return;
            foreach (var column in database.Tables.SelectMany(table => table.Columns))
            {
                ProcessColumn(column);
            }
        }

        public void ProcessColumn(DBColumn dbColumn)
        {
            ConvertChar38ToGuid(dbColumn);
        }

        private static void ConvertChar38ToGuid(DBColumn dbColumn)
        {
            if (dbColumn.DataType != DBDataTypes.CHAR || dbColumn.DataLength != 38) return;
            dbColumn.DataType = DBDataTypes.UNIQUEIDENTIFIER;
            dbColumn.DataLength = 0;
        }
    }
}